/*
 * @Description:
 * @Version:
 * @Author: Troubadour
 * @Date: 2023-11-13 09:29:41
 * @LastEditors: Troubadour
 * @LastEditTime: 2023-12-10 16:11:49
 */

#include "bsp_SDdriver.h"

MSD_CARDINFO Bsp_SDCardInfo;

/**
 * @function: Bsp_SD_Set_SPI_Baud
 * @brief Set SPI baud
 * @param {uint32_t} Baud: (SPI_BAUDRATEPRESCALER_256) or (SPI_BAUDRATEPRESCALER_4)
 * @return {*}
 */
static void Bsp_SD_Set_SPI_Baud(uint32_t Baud)
{
    // __HAL_SPI_DISABLE(&SD_SPI_HANDLE);
    // SD_SPI_HANDLE.Instance->CR1 &= 0xFFC7;
    // SD_SPI_HANDLE.Instance->CR1 |= Baud << 3;
    // __HAL_SPI_ENABLE(&SD_SPI_HANDLE);
    SD_SPI_HANDLE.Instance->CR1 = Baud;
    HAL_SPI_Init(&SD_SPI_HANDLE);
}

/**
 * @function: Bsp_SD_ReadWriteOneByte
 * @brief 同时读写一个字节
 * @param {uint8_t} DataTx: 要发送的字节
 * @return {uint8_t} 读取的字节
 */
static uint8_t Bsp_SD_ReadWriteOneByte(uint8_t DataTx)
{
    uint8_t DataRx;

#if ENABLIE_HARD_SPI
    HAL_SPI_TransmitReceive(&SD_SPI_HANDLE, &DataTx, &DataRx, 1, 10);
#else

#endif
    return DataRx;
}

/**
 * @function: Bsp_SD_WriteByte
 * @brief 写入一个字节
 * @param {uint8_t} TxData: 要写入的字节
 * @return {*}
 */
static HAL_StatusTypeDef Bsp_SD_WriteByte(uint8_t TxData)
{
#if ENABLIE_HARD_SPI
    return HAL_SPI_Transmit(&SD_SPI_HANDLE, &TxData, 1, 10);
#else

#endif
}

/**
 * @function: Bsp_SD_ReadWait
 * @brief 读取等待，直到接收 data 停下
 * @param {uint8_t} data: 要接收的数据
 * @param {uint16_t} timeout: 超时次数
 * @return {uint8_t} 返回读取结果
 */
static uint8_t Bsp_SD_ReadWait(uint8_t data, uint16_t timeout)
{
    uint8_t result = 0;

    do
    {
        result = Bsp_SD_ReadWriteOneByte(0xFF);
    } while ((result != data) && (timeout--));

    return result;
}

/**
 * @function: Bsp_SD_ReadBlock
 * @brief 读取块的数据
 * @param {uint8_t} *buff: 数据存储区
 * @param {uint32_t} len: 读取的长度
 * @return {*} 返回 HAL_OK 成功，其它失败
 */
HAL_StatusTypeDef Bsp_SD_ReadBlock(uint8_t *buff, uint32_t len)
{
    SD_SPI_CS(0);

    if (Bsp_SD_ReadWait(0xFE, 200) != 0xFE)
    {
        SD_SPI_CS(1);
        return HAL_ERROR;
    }

    while (len--)
    {
        *buff = Bsp_SD_ReadWriteOneByte(0xFF);
        buff++;
    }

    Bsp_SD_ReadWriteOneByte(0xFF);
    Bsp_SD_ReadWriteOneByte(0xFF);
    SD_SPI_CS(1);
    return HAL_OK;
}

/**
 * @function: Bsp_SD_ReceiveDataBuffer
 * @brief  读取指定长度数据
 * @param {uint8_t} *data: Receive data buffer
 * @param {uint16_t} len: Receive data length
 * @return {*}
 */
HAL_StatusTypeDef Bsp_SD_ReceiveDataBuffer(uint8_t *data, uint16_t len)
{
    SD_SPI_CS(0);
    if (Bsp_SD_ReadWait(0xFE, 200) != 0xFE)
    {
        return HAL_ERROR;
    }

    while (len--)
    {
        *data = Bsp_SD_ReadWriteOneByte(0xFF);
        data++;
    }
    Bsp_SD_ReadWriteOneByte(0xFF);
    Bsp_SD_ReadWriteOneByte(0xFF);
    SD_SPI_CS(1);

    return HAL_OK;
}

/**
 * @function: Bsp_SD_WriteBlock
 * @brief 向sd卡写入一个块的内容 SDCard_SECTOR_SIZE 字节
 * @param {uint8_t} *buff: 数据
 * @param {uint8_t} cmd: 命令
 * @return {*}
 */
HAL_StatusTypeDef Bsp_SD_WriteBlock(uint8_t *buff, uint8_t cmd)
{
    uint8_t result = 0;
    uint16_t i = 0;
    uint16_t TimeOut = 0;

    SD_SPI_CS(0);
    /* wait SD ready */
    if (Bsp_SD_ReadWait(0xFF, 200) != 0xFF)
    {
        SD_SPI_CS(1);
        return HAL_ERROR;
    }

    /* transmit token */
    Bsp_SD_ReadWriteOneByte(cmd);

    if (cmd != 0xFD) // 不是结束指令
    {
        for (i = 0; i < SDCard_SECTOR_SIZE; i++)
        {
            Bsp_SD_WriteByte(buff[i]);
        }
        Bsp_SD_WriteByte(0xFF); // 忽略crc
        Bsp_SD_WriteByte(0xFF);

        TimeOut = 200;
        while (TimeOut--)
        {
            result = Bsp_SD_ReadWriteOneByte(0xFF); // 接收响应
            if ((result & 0x1F) == 0x05)
            {
                break;
            }
        }
        Bsp_SD_ReadWait(0, 1000);
        SD_SPI_CS(1);

        if (TimeOut == 0)
        {
            return HAL_ERROR;
        }
    }

    SD_SPI_CS(1);
    return HAL_OK;
}

/**
 * @function: Bsp_SD_SendCmd
 * @brief SD发送命令函数
 * @param {uint8_t} cmd: 命令
 * @param {uint32_t} arg: 参数
 * @param {uint8_t} crc: CRC校验
 * @param {uint8_t} *result: 返回值
 * @return {*}返回 HAL_OK 成功，其它失败
 */
HAL_StatusTypeDef Bsp_SD_SendCmd(uint8_t cmd, uint32_t arg, uint8_t crc, uint8_t *result)
{
    uint16_t CntTime = 0;

    SD_SPI_CS(1);                 // 取消上次片选
    Bsp_SD_ReadWriteOneByte(0xFF); // 提供额外的8个时钟
    SD_SPI_CS(0);                 // 选中SD卡
    /* 等待成功 */
    CntTime = 0;
    while (Bsp_SD_ReadWriteOneByte(0xFF) != 0xFF)
    {
        if (CntTime++ >= 1000)
        {
            SD_SPI_CS(1);
            return HAL_ERROR;
        }
    }
    /* 发送数据 */
    Bsp_SD_ReadWriteOneByte(cmd | 0x40); // 分别写入命令
    Bsp_SD_ReadWriteOneByte(arg >> 24);
    Bsp_SD_ReadWriteOneByte(arg >> 16);
    Bsp_SD_ReadWriteOneByte(arg >> 8);
    Bsp_SD_ReadWriteOneByte(arg);
    Bsp_SD_ReadWriteOneByte(crc);

    if (cmd == SDCard_CMD12)
    {
        Bsp_SD_ReadWriteOneByte(0xFF);
    }

    /* 等待响应，或超时退出 */
    CntTime = 0;
    do
    {
        *result = Bsp_SD_ReadWriteOneByte(0xFF);
        if (CntTime++ >= 1000)
        {
            SD_SPI_CS(1);
            return HAL_ERROR;
        }
    } while (*result & 0x80);

    SD_SPI_CS(1);
    return HAL_OK;
}

/**
 * @function: Bsp_SD_GETCID
 * @brief: 获取长度16字节的CID
 * @param {uint8_t} *cid_data: 存储长度为16字节的容器
 * @return {*} HAL_OK 成功，其它失败
 */
HAL_StatusTypeDef Bsp_SD_GETCID(uint8_t *cid_data)
{
    uint8_t result = 0;

    SD_SPI_CS(0);

    if (Bsp_SD_SendCmd(SDCard_CMD10, 0, 0x01, &result) != HAL_OK)
    {
        return HAL_ERROR;
    }

    if (result == 0x00)
    {
        if (Bsp_SD_ReadBlock(cid_data, 16) != HAL_OK)
        {
            return HAL_ERROR;
        }
    }

    SD_SPI_CS(1);
    return HAL_OK;
}

/**
 * @function: Bsp_SD_GetCardInfo
 * @brief 获取卡信息
 * @param {PMSD_CARDINFO} Bsp_SDCardInfo
 * @return {*}
 */
HAL_StatusTypeDef Bsp_SD_GetCardInfo(PMSD_CARDINFO Bsp_SDCardInfo)
{
    uint8_t result;
    uint8_t CSD_Tab[16];
    uint8_t CID_Tab[16];

    SD_SPI_CS(0);

    /* Get CSD data */
    if ((Bsp_SD_SendCmd(SDCard_CMD9, 0, 0xFF, &result) != (HAL_OK) || (result != 0x00)))
    {
        return HAL_ERROR;
    }
    if (Bsp_SD_ReceiveDataBuffer(CSD_Tab, 16) != HAL_OK)
    {
        return HAL_ERROR;
    }
    /* Get CID data */
    if ((Bsp_SD_SendCmd(SDCard_CMD10, 0, 0xFF, &result) != (HAL_OK) || (result != 0x00)))
    {
        return HAL_ERROR;
    }
    if (Bsp_SD_ReceiveDataBuffer(CID_Tab, 16) != HAL_OK)
    {
        return HAL_ERROR;
    }

    /* Byte 0 */
    Bsp_SDCardInfo->CSD.CSDStruct = (CSD_Tab[0] & 0xC0) >> 6;
    Bsp_SDCardInfo->CSD.SysSpecVersion = (CSD_Tab[0] & 0x3C) >> 2;
    Bsp_SDCardInfo->CSD.Reserved1 = CSD_Tab[0] & 0x03;
    /* Byte 1 */
    Bsp_SDCardInfo->CSD.TAAC = CSD_Tab[1];
    /* Byte 2 */
    Bsp_SDCardInfo->CSD.NSAC = CSD_Tab[2];
    /* Byte 3 */
    Bsp_SDCardInfo->CSD.MaxBusClkFrec = CSD_Tab[3];
    /* Byte 4 */
    Bsp_SDCardInfo->CSD.CardComdClasses = CSD_Tab[4] << 4;
    /* Byte 5 */
    Bsp_SDCardInfo->CSD.CardComdClasses |= (CSD_Tab[5] & 0xF0) >> 4;
    Bsp_SDCardInfo->CSD.RdBlockLen = CSD_Tab[5] & 0x0F;
    /* Byte 6 */
    Bsp_SDCardInfo->CSD.PartBlockRead = (CSD_Tab[6] & 0x80) >> 7;
    Bsp_SDCardInfo->CSD.WrBlockMisalign = (CSD_Tab[6] & 0x40) >> 6;
    Bsp_SDCardInfo->CSD.RdBlockMisalign = (CSD_Tab[6] & 0x20) >> 5;
    Bsp_SDCardInfo->CSD.DSRImpl = (CSD_Tab[6] & 0x10) >> 4;
    Bsp_SDCardInfo->CSD.Reserved2 = 0; /* Reserved */
    Bsp_SDCardInfo->CSD.DeviceSize = (CSD_Tab[6] & 0x03) << 10;
    /* Byte 7 */
    Bsp_SDCardInfo->CSD.DeviceSize |= (CSD_Tab[7]) << 2;
    /* Byte 8 */
    Bsp_SDCardInfo->CSD.DeviceSize |= (CSD_Tab[8] & 0xC0) >> 6;
    Bsp_SDCardInfo->CSD.MaxRdCurrentVDDMin = (CSD_Tab[8] & 0x38) >> 3;
    Bsp_SDCardInfo->CSD.MaxRdCurrentVDDMax = (CSD_Tab[8] & 0x07);
    /* Byte 9 */
    Bsp_SDCardInfo->CSD.MaxWrCurrentVDDMin = (CSD_Tab[9] & 0xE0) >> 5;
    Bsp_SDCardInfo->CSD.MaxWrCurrentVDDMax = (CSD_Tab[9] & 0x1C) >> 2;
    Bsp_SDCardInfo->CSD.DeviceSizeMul = (CSD_Tab[9] & 0x03) << 1;
    /* Byte 10 */
    Bsp_SDCardInfo->CSD.DeviceSizeMul |= (CSD_Tab[10] & 0x80) >> 7;
    Bsp_SDCardInfo->CSD.EraseGrSize = (CSD_Tab[10] & 0x7C) >> 2;
    Bsp_SDCardInfo->CSD.EraseGrMul = (CSD_Tab[10] & 0x03) << 3;
    /* Byte 11 */
    Bsp_SDCardInfo->CSD.EraseGrMul |= (CSD_Tab[11] & 0xE0) >> 5;
    Bsp_SDCardInfo->CSD.WrProtectGrSize = (CSD_Tab[11] & 0x1F);
    /* Byte 12 */
    Bsp_SDCardInfo->CSD.WrProtectGrEnable = (CSD_Tab[12] & 0x80) >> 7;
    Bsp_SDCardInfo->CSD.ManDeflECC = (CSD_Tab[12] & 0x60) >> 5;
    Bsp_SDCardInfo->CSD.WrSpeedFact = (CSD_Tab[12] & 0x1C) >> 2;
    Bsp_SDCardInfo->CSD.MaxWrBlockLen = (CSD_Tab[12] & 0x03) << 2;
    /* Byte 13 */
    Bsp_SDCardInfo->CSD.MaxWrBlockLen |= (CSD_Tab[13] & 0xc0) >> 6;
    Bsp_SDCardInfo->CSD.WriteBlockPaPartial = (CSD_Tab[13] & 0x20) >> 5;
    Bsp_SDCardInfo->CSD.Reserved3 = 0;
    Bsp_SDCardInfo->CSD.ContentProtectAppli = (CSD_Tab[13] & 0x01);
    /* Byte 14 */
    Bsp_SDCardInfo->CSD.FileFormatGrouop = (CSD_Tab[14] & 0x80) >> 7;
    Bsp_SDCardInfo->CSD.CopyFlag = (CSD_Tab[14] & 0x40) >> 6;
    Bsp_SDCardInfo->CSD.PermWrProtect = (CSD_Tab[14] & 0x20) >> 5;
    Bsp_SDCardInfo->CSD.TempWrProtect = (CSD_Tab[14] & 0x10) >> 4;
    Bsp_SDCardInfo->CSD.FileFormat = (CSD_Tab[14] & 0x0C) >> 2;
    Bsp_SDCardInfo->CSD.ECC = (CSD_Tab[14] & 0x03);
    /* Byte 15 */
    Bsp_SDCardInfo->CSD.CSD_CRC = (CSD_Tab[15] & 0xFE) >> 1;
    Bsp_SDCardInfo->CSD.Reserved4 = 1;

    if (Bsp_SDCardInfo->CardType == SDCard_TYPE_V2HC)
    {
        /* Byte 7 */
        Bsp_SDCardInfo->CSD.DeviceSize = (uint16_t)(CSD_Tab[8]) * 256;
        /* Byte 8 */
        Bsp_SDCardInfo->CSD.DeviceSize += CSD_Tab[9];
    }

    Bsp_SDCardInfo->Capacity = Bsp_SDCardInfo->CSD.DeviceSize * SDCard_SECTOR_SIZE * 1024;
    Bsp_SDCardInfo->BlockSize = SDCard_SECTOR_SIZE;

    /* Byte 0 */
    Bsp_SDCardInfo->CID.ManufacturerID = CID_Tab[0];
    /* Byte 1 */
    Bsp_SDCardInfo->CID.OEM_AppliID = CID_Tab[1] << 8;
    /* Byte 2 */
    Bsp_SDCardInfo->CID.OEM_AppliID |= CID_Tab[2];
    /* Byte 3 */
    Bsp_SDCardInfo->CID.ProdName1 = CID_Tab[3] << 24;
    /* Byte 4 */
    Bsp_SDCardInfo->CID.ProdName1 |= CID_Tab[4] << 16;
    /* Byte 5 */
    Bsp_SDCardInfo->CID.ProdName1 |= CID_Tab[5] << 8;
    /* Byte 6 */
    Bsp_SDCardInfo->CID.ProdName1 |= CID_Tab[6];
    /* Byte 7 */
    Bsp_SDCardInfo->CID.ProdName2 = CID_Tab[7];
    /* Byte 8 */
    Bsp_SDCardInfo->CID.ProdRev = CID_Tab[8];
    /* Byte 9 */
    Bsp_SDCardInfo->CID.ProdSN = CID_Tab[9] << 24;
    /* Byte 10 */
    Bsp_SDCardInfo->CID.ProdSN |= CID_Tab[10] << 16;
    /* Byte 11 */
    Bsp_SDCardInfo->CID.ProdSN |= CID_Tab[11] << 8;
    /* Byte 12 */
    Bsp_SDCardInfo->CID.ProdSN |= CID_Tab[12];
    /* Byte 13 */
    Bsp_SDCardInfo->CID.Reserved1 |= (CID_Tab[13] & 0xF0) >> 4;
    /* Byte 14 */
    Bsp_SDCardInfo->CID.ManufactDate = (CID_Tab[13] & 0x0F) << 8;
    /* Byte 15 */
    Bsp_SDCardInfo->CID.ManufactDate |= CID_Tab[14];
    /* Byte 16 */
    Bsp_SDCardInfo->CID.CID_CRC = (CID_Tab[15] & 0xFE) >> 1;
    Bsp_SDCardInfo->CID.Reserved2 = 1;

    SD_SPI_CS(1);
    return HAL_OK;
}

/**
 * @function: Bsp_SD_WriteDisk
 * @brief: 写入SD卡数据，SDCard_SECTOR_SIZE字节
 * @param {uint8_t} *buf: 数据
 * @param {uint32_t} sector: 扇区
 * @param {uint32_t} cnt: 数量
 * @return {*} HAL_OK 成功，其它失败
 */
HAL_StatusTypeDef Bsp_SD_WriteDisk(uint8_t *buf, uint32_t sector, uint32_t cnt)
{
    uint8_t result = 0;

    if (Bsp_SDCardInfo.CardType != SDCard_TYPE_V2HC)
    {
        sector <<= 9; // 转换为字节地址
    }
    SD_SPI_CS(0);

    if (cnt == 1)
    {
        if (Bsp_SD_SendCmd(SDCard_CMD24, sector, 0X01, &result) != HAL_OK) // 写命令
        {
            return HAL_ERROR;
        }
        if (result == 0) // 指令发送成功
        {
            // 发送停止命令
            if (Bsp_SD_WriteBlock(buf, 0xFE) != HAL_OK)
            {
                cnt = 1;
                return HAL_ERROR;
            }
            else
            {
                cnt = 0;
            }
        }
    }
    else
    {
        if (Bsp_SDCardInfo.CardType != SDCard_TYPE_MMC)
        {
            if ((Bsp_SD_SendCmd(SDCard_CMD55, 0, 0x01, &result) != HAL_OK) ||
            (Bsp_SD_SendCmd(SDCard_CMD23, cnt, 0x01, &result) != HAL_OK)) /* 连续读命令 */
            {
                return HAL_ERROR;
            }
        }

        if ((Bsp_SD_SendCmd(SDCard_CMD25, sector, 0x01, &result) != HAL_OK))
        {
            return HAL_ERROR;
        }
        if (result == 0)
        {
            do
            {
                if (Bsp_SD_WriteBlock(buf, 0xFC) != HAL_OK) // 发送SDCard_SECTOR_SIZE个字节
                {
                    break;
                }
                buf += SDCard_SECTOR_SIZE;
            } while (--cnt);
        }

        if (!Bsp_SD_WriteBlock(0, 0xFD))
        {
            cnt = 1;
        }
    }

    SD_SPI_CS(1); // 取消片选
    Bsp_SD_ReadWriteOneByte(0xFF);
    return (cnt == 0) ? (HAL_OK) : (HAL_ERROR);
}

/**
 * @function: Bsp_SD_ReadDisk
 * @brief  读SD卡
 * @param {uint8_t} *buf: 数据缓存区
 * @param {uint32_t} sector: 扇区
 * @param {uint8_t} cnt: 扇区数
 * @return {*} HAL_OK 成功，其它失败
 */
HAL_StatusTypeDef Bsp_SD_ReadDisk(uint8_t *buf, uint32_t sector, uint32_t cnt)
{
    uint8_t result = 0;

    if (!(Bsp_SDCardInfo.CardType & CT_BLOCK))
    {
        sector *= 512; // 转换为字节地址
    }

    SD_SPI_CS(0);
    if (cnt == 1)
    {
        if (Bsp_SD_SendCmd(SDCard_CMD17, sector, 0X01, &result) != HAL_OK) // 读命令
        {
            return HAL_ERROR;
        }
        if ((result == 0) && (Bsp_SD_ReadBlock(buf, SDCard_SECTOR_SIZE) == HAL_OK)) // 指令发送成功
        {
            cnt = 0;
        }
    }
    else
    {
        // 连续读命令
        if ((Bsp_SD_SendCmd(SDCard_CMD18, sector, 0X01, &result) == HAL_OK) &&
            (result == 0))
        {
            do
            {
                if (Bsp_SD_ReadBlock(buf, SDCard_SECTOR_SIZE) != HAL_OK) // 接收SDCard_SECTOR_SIZE个字节
                {
                    break;
                }
                buf += SDCard_SECTOR_SIZE;
            } while (--cnt);
            // 发送停止命令
            if (Bsp_SD_SendCmd(SDCard_CMD12, 0, 0X01, &result) != HAL_OK)
            {
                cnt = 1;
            }
        }

    }

    SD_SPI_CS(1); // 取消片选
    Bsp_SD_ReadWriteOneByte(0xFF);
    return (cnt) ? (HAL_ERROR) : (HAL_OK);
}

/**
 * @function: Bsp_SD_Init
 * @brief SD卡初始化
 * @param {uint8_t} *ret: 卡类型，不需要查看给NULL
 * @return {*} HAL_OK 成功，其它失败
 */
HAL_StatusTypeDef Bsp_SD_Init(void)
{
    uint16_t i = 0;
    uint8_t result = 0;
    uint8_t buff[6] = {0};
    uint16_t CntTime = 0;

    Bsp_SD_Set_SPI_Baud(SPI_BAUDRATEPRESCALER_256);
    SD_SPI_CS(0);
    /* 发送最少74个脉冲 */
    for (i = 0; i < 10; i++)
    {
        Bsp_SD_ReadWriteOneByte(0xFF);
    }

    /* 进入闲置状态 */
    CntTime = 0;
    do
    {
        if ((Bsp_SD_SendCmd(SDCard_CMD0, 0, 0x95, &result) != HAL_OK) || (CntTime++ >= 1000))
        {
            printf("IDLE Error \r\n");
            return HAL_ERROR;
        }
    } while (result != 0x01);

    Bsp_SDCardInfo.CardType = 0;
    /* 鉴别是否是2.0版本的SD卡 */
    if (Bsp_SD_SendCmd(SDCard_CMD8, 0x1AA, 0x87, &result) != HAL_OK)
    {
        return HAL_ERROR;
    }
    if (result == 0x01)
    {
        for (i = 0; i < 4; i++)
        {
            buff[i] = Bsp_SD_ReadWriteOneByte(0xFF);
        }

        if (buff[2] == 0X01 && buff[3] == 0XAA) // 卡是否支持2.7~3.6V
        {
            uint8_t temp = 0;

            CntTime = 0;
            do
            {
                // 发送SDCard_CMD55, 发送SDCard_CMD41
                if ((Bsp_SD_SendCmd(SDCard_CMD55, 0, 0x01, &result) != HAL_OK) || (Bsp_SD_SendCmd(SDCard_CMD41, 0x40000000, 0x01, &result) != HAL_OK) || (CntTime++ >= 1000))
                {
                    return HAL_ERROR;
                }
            } while (result);

            // 鉴别SD2.0卡版本开始
            if (Bsp_SD_SendCmd(SDCard_CMD58, 0, 0x01, &temp) != HAL_OK)
            {
                return HAL_ERROR;
            }
            if (result && (temp == 0))
            {
                // 得到OCR值
                for (i = 0; i < 4; i++)
                {
                    buff[i] = Bsp_SD_ReadWriteOneByte(0xFF);
                }
                if (buff[0] & 0x40)
                {
                    Bsp_SDCardInfo.CardType = SDCard_TYPE_V2HC;
                }
                else
                {
                    Bsp_SDCardInfo.CardType = SDCard_TYPE_V2;
                }
            }
        }
        else
        {
            // 发送SDCard_CMD55, 发送SDCard_CMD41
            if ((Bsp_SD_SendCmd(SDCard_CMD55, 0, 0x01, &result) != HAL_OK) || (Bsp_SD_SendCmd(SDCard_CMD41, 0, 0x01, &result) != HAL_OK))
            {
                return HAL_ERROR;
            }
            if (result <= 1)
            {
                Bsp_SDCardInfo.CardType = SDCard_TYPE_V1;
                // 等待退出IDLE模式
                CntTime = 0;
                do
                {
                    // 发送SDCard_CMD55, 发送SDCard_CMD41
                    if ((Bsp_SD_SendCmd(SDCard_CMD55, 0, 0x01, &result) != HAL_OK) || (Bsp_SD_SendCmd(SDCard_CMD41, 0, 0x01, &result) != HAL_OK) || (CntTime++ >= 1000))
                    {
                        return HAL_ERROR;
                    }
                } while (result);
            }
            else // MMC卡不支持CMD55+CMD41识别
            {
                Bsp_SDCardInfo.CardType = SDCard_TYPE_MMC;
                do
                {
                    // 发送SDCard_CMD55, 发送SDCard_CMD41
                    if ((Bsp_SD_SendCmd(SDCard_CMD1, 0, 0x01, &result) != HAL_OK) || (CntTime++ >= 1000))
                    {
                        return HAL_ERROR;
                    }
                } while (result);
            }
        }
    }

    Bsp_SD_Set_SPI_Baud(SPI_BAUDRATEPRESCALER_16);

    SD_SPI_CS(1);
    Bsp_SD_ReadWriteOneByte(0xFF);
    return HAL_OK;
}
